package v1

import (
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/repository"
	"RoccBlog/util"
)

type PermissionRepository struct {
	repository.BaseRepository
}

func (r *PermissionRepository) Create(data model.Permission) error {
	return global.DB.Create(&data).Error
}

func (r *PermissionRepository) Delete(id int) error {
	return global.DB.Delete(&model.Permission{}, id).Error
}

func (r *PermissionRepository) Update(data model.Permission) error {
	return global.DB.Model(&data).Updates(&data).Error
}

func (r *PermissionRepository) Select(pageNum, pageSize int) (any, error) {
	var (
		list  []model.Permission
		count int64
		err   error
	)
	db := global.DB.Model(&model.Permission{})

	err = db.Count(&count).Error
	if err != nil {
		return nil, err
	}

	err = db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&list).Error

	return util.ToPageInfo(count, pageSize, pageNum, list), err
}

func (r *PermissionRepository) SelectById(id int) (model.Permission, error) {
	var permission model.Permission
	err := global.DB.Take(&permission, id).Error
	return permission, err
}
func (r *PermissionRepository) SelectByIds(ids []int) ([]model.Permission, error) {
	var list []model.Permission
	err := global.DB.Where("id in(?)", ids).Find(&list).Error
	return list, err
}
